package com.mrd.base.utils;

import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

public class StringUtils extends org.apache.commons.lang.StringUtils {
  /** The FieldPosition. */
  private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

  private static final String defaultPattern = "yyyyMMdd";

  /** This Format for format the number to special format. */
  private final static NumberFormat numberFormat = new DecimalFormat("000");

  /** This int is the sequence number ,the default value is 0. */
  private static int seq = 0;

  private static final int MAX = 999;

  // 首字母转小写
  public static String toLowerCaseFirstOne(String s) {
    if (Character.isLowerCase(s.charAt(0)))
      return s;
    else
      return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1))
          .toString();
  }

  // 首字母转大写
  public static String toUpperCaseFirstOne(String s) {

    if (Character.isUpperCase(s.charAt(0)))
      return s;
    else
      return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1))
          .toString();
  }

  /**
   * 判断是否是空字符串 null和"" 都返回 true
   * 
   * @author Robin Chang
   * @param s
   * @return
   */
  public static boolean isEmpty(String s) {
    if (s != null && !s.equals("")) {
      return false;
    }
    return true;
  }

  public static void printJson(HttpServletResponse response, Object content) {
    try {
      response.reset();
      response.setContentType("application/json");
      response.setHeader("Cache-Control", "no-store");
      response.setCharacterEncoding("UTF-8");
      PrintWriter pw = response.getWriter();
      String json = JSONObject.toJSONString(content);
      pw.print(json);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void printJson(HttpServletResponse response, String content) {
    try {
      response.reset();
      response.setContentType("application/json");
      response.setHeader("Cache-Control", "no-store");
      response.setCharacterEncoding("UTF-8");
      PrintWriter pw = response.getWriter();
      pw.write(content);
      pw.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 判断是否为数字
   * 
   * @Title: isNumeric
   * @Description: 判断是否为数字
   * @param str
   * @return
   * @return: boolean
   */
  public static boolean isNumeric(String str) {
    for (int i = 0; i < str.length(); i++) {
      if (!Character.isDigit(str.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  /**
   * 
   * @title: camelToUnderline
   * @description: 驼峰转下划线
   * @param param
   * @return
   * @return: String
   */
  public static String camelToUnderline(String param) {
    if (param == null || "".equals(param.trim())) {
      return "";
    }
    int len = param.length();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++) {
      char c = param.charAt(i);
      if (Character.isUpperCase(c)) {
        sb.append('_');
        sb.append(Character.toLowerCase(c));
      } else {
        sb.append(c);
      }
    }
    return sb.toString();
  }

  /**
   * 
   * @title: underlineToCamel
   * @description:下划线转驼峰
   * @param param
   * @return
   * @return: String
   */
  public static String underlineToCamel(String param) {
    if (param == null || "".equals(param.trim())) {
      return "";
    }
    int len = param.length();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++) {
      char c = param.charAt(i);
      if (c == '_') {
        if (++i < len) {
          sb.append(Character.toUpperCase(param.charAt(i)));
        }
      } else {
        sb.append(c);
      }
    }
    return sb.toString();
  }

  /**
   * A hashing method that changes a string (like a URL) into a hash suitable for using as a disk
   * filename.
   */
  public static String hashKeyForDisk(String key) {
    String cacheKey;
    try {
      final MessageDigest mDigest = MessageDigest.getInstance("MD5");
      mDigest.update(key.getBytes());
      cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
      cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
  }

  private static String bytesToHexString(byte[] bytes) {
    // http://stackoverflow.com/questions/332079
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < bytes.length; i++) {
      String hex = Integer.toHexString(0xFF & bytes[i]);
      if (hex.length() == 1) {
        sb.append('0');
      }
      sb.append(hex);
    }
    return sb.toString();
  }

  /**
   * 
   * @title: getExtensionName
   * @description: Java文件操作 获取文件扩展名
   * @param filename
   * @return
   * @return: String
   */
  public static String getExtensionName(String filename) {
    if ((filename != null) && (filename.length() > 0)) {
      int dot = filename.lastIndexOf('.');
      if ((dot > -1) && (dot < (filename.length() - 1))) {
        return filename.substring(dot + 1);
      }
    }
    return filename;
  }

  /**
   * 
   * @title: getFileNameNoEx
   * @description: Java文件操作 获取不带扩展名的文件名
   * @param filename
   * @return
   * @return: String
   */
  public static String getFileNameNoEx(String filename) {
    if ((filename != null) && (filename.length() > 0)) {
      int dot = filename.lastIndexOf('.');
      if ((dot > -1) && (dot < (filename.length()))) {
        return filename.substring(0, dot);
      }
    }
    return filename;
  }

  public static String randomUUID() {
    UUID uuid = UUID.randomUUID();
    return uuid.toString().replace("-", "");
  }

  public static boolean isNumericAndDot(String str) {
    if (str == null || str.trim().equals("")) {
      return false;
    }
    Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]*([Ee]{1}[0-9]+)?");
    Matcher isNum = pattern.matcher(str);
    if (!isNum.matches()) {
      return false;
    }
    return true;
  }

  /**
   * 去除字符串首尾出现的某个字符.
   * 
   * @param source 源字符串.
   * @param element 需要去除的字符.
   * @return String.
   */
  public static String trimFirstAndLastChar(String source, char element) {
    boolean beginIndexFlag = true;
    boolean endIndexFlag = true;
    do {
      int beginIndex = source.indexOf(element) == 0 ? 1 : 0;
      int endIndex =
          source.lastIndexOf(element) + 1 == source.length() ? source.lastIndexOf(element)
              : source.length();
      source = source.substring(beginIndex, endIndex);
      beginIndexFlag = (source.indexOf(element) == 0);
      endIndexFlag = (source.lastIndexOf(element) + 1 == source.length());
    } while (beginIndexFlag || endIndexFlag);
    return source;
  }

  /**
   * 替换掉HTML标签方法
   */
  public static String replaceHtml(String html) {
    if (isBlank(html)) {
      return "";
    }
    String regEx = "<.+?>";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(html);
    String s = m.replaceAll("");
    return s;
  }

  /**
   * 按时间格式生成id
   * 
   * @return String
   */
  public static synchronized String generateSequenceNo(String prefix, String pattern) {
    if (StringUtils.isEmpty(pattern)) {
      pattern = defaultPattern;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    Calendar rightNow = Calendar.getInstance();

    StringBuffer sb = new StringBuffer();

    dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);

    numberFormat.format(seq, sb, HELPER_POSITION);

    if (seq == MAX) {
      seq = 0;
    } else {
      seq++;
    }
    return prefix + sb.toString();
  }

  public static synchronized String generateRadomSequenceNo(String prefix) {
    return generateSequenceNo(prefix, "yyyyMMddhhmmssSSS");
  }
}
